from torch_scatter import scatter_add
import torch

edge_index = torch.LongTensor([[0, 0, 1, 2, 2, 3, 4], [5, 7, 5, 6, 7, 6, 6]])
dtype = edge_index.dtype
edge_weight = torch.FloatTensor([0.01, 0.02, 0.1, 0.2, 0.21, 0.3, 0.4])
# edge_weight = torch.ones((edge_index.size(1),),
#                          dtype=dtype,
#                          device=edge_index.device)
print(edge_weight,"1")
edge_weight = edge_weight.view(-1)
print(edge_weight,"2")
num_nodes = 8
#
# 根据提供的索引（row）将源张量（edge_weight）中的值累加到目标张量（deg）的相应位置上，用来计算度矩阵是十分完美的操作。
deg = scatter_add(edge_weight.clone(), edge_index[0], dim=0, dim_size=num_nodes)
# 度矩阵(太完美了)
print(deg,"3")
deg_inv_sqrt = deg.pow(-1) # 取到数
print(deg_inv_sqrt,"4")
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
print(deg_inv_sqrt,"5")
print(deg_inv_sqrt[edge_index[0]], deg_inv_sqrt[edge_index[1]])